import axios from 'axios';
import PubSub from 'pubsub-js';
import {TOAST, LOADING, LOGIN} from '@/util/event-types';
import storage from '@/util/localStorage';
import qs from 'qs';

const service = axios.create();

//service.defaults.baseURL = 'https://server.americansunion.com/';
service.defaults.baseURL = 'https://d162yf00fuggac.cloudfront.net/';

service.interceptors.response.use(
    response => {
        PubSub.publish(LOADING, false);
        const {status} = response;
        if (status === 200) {
            return Promise.resolve(response);
        }
        return Promise.reject(response);
    },
    error => {
        PubSub.publish(LOADING, false);
        if (error.response) {
            const status = parseInt(error.response.data.status);
            if (status === 401 || status === 403) {
                PubSub.publish(LOGIN);
            }
        }
        return Promise.reject(error);
    },
);

service.interceptors.request.use(
    function (config) {
        PubSub.publish(LOADING, true);
        return new Promise((resolve, _reject) => {
            storage.getData('TOKEN_INFO').then(d => {
                if (d !== null) {
                    config.headers = {
                        'Content-Type': 'application/json',
                        ...d
                    };
                }
                resolve(config);
            });
        });
    },
    function (error) {
        PubSub.publish(LOADING, false);
        return Promise.reject(error);
    },
);

export default {
    get(url, param) {
        return new Promise((resolve, reject) => {
            service({
                method: 'get',
                url,
                params: param,
            })
                .then(res => {
                    const data = res.data;
                    if (data && data !== '') {
                        resolve(data);
                    } else {
                        resolve(res);
                    }
                })
                .catch(_err => {
                });
        });
    },
    //post request
    post(url, param, formData: false) {
        return new Promise((resolve, reject) => {
            service({
                method: 'post',
                url,
                data: formData ? qs.stringify(param) : param,
            })
                .then(res => {
                    const data = res.data;
                    if (data && data !== '') {
                        resolve(data);
                    } else {
                        resolve(res);
                    }
                })
                .catch(_err => {
                });
        });
    },
    uploadFile(url, param) {
        return new Promise((resolve, reject) => {
            service({
                method: 'post',
                url,
                data: param,
                headers: {
                    'Content-Type': 'multipart/form-data',
                },
            })
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    console.log(err, 'exception');
                });
        });
    },
};
